<?php

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$

class Page extends Think {

	// 分页栏每页显示的页数
	public $rollPage = 9;
	// 页数跳转时要带的参数
	public $parameter;
	// 默认列表每页显示行数
	protected $listRows = 20;
	// 起始行数
	protected $firstRow;
	// 分页总页面数
	protected $totalPages;
	// 总行数
	protected $totalRows;
	// 当前页数
	protected $nowPage;
	// 分页的栏的总页数
	protected $coolPages;
	// 分页显示定制
	protected $config = array('header' => '条记录', 'prev' => '前页', 'next' => '后页', 'first' => '首页', 'last' => '尾页', 'theme' => '<span class="p_info"><span class="p_total">%totalRow% %header%</span> <span class="p_size">%nowPage%/%totalPage% 页</span></span> <span class="page_contorl"> %prePage% %first% %linkPage% %end% %nextPage%</span>');

	/**
	  +----------------------------------------------------------
	 * 架构函数
	  +----------------------------------------------------------
	 * @access public
	  +----------------------------------------------------------
	 * @param array $totalRows  总的记录数
	 * @param array $listRows  每页显示记录数
	 * @param array $parameter  分页跳转的参数
	  +----------------------------------------------------------
	 */
	public function __construct($totalRows, $listRows='', $parameter='') {
		$this->totalRows = $totalRows;
		$this->parameter = $parameter;
		if (!empty($listRows)) {
			$this->listRows = intval($listRows);
		}
		$this->totalPages = ceil($this->totalRows / $this->listRows);  //总页数
		$this->coolPages = ceil($this->totalPages / $this->rollPage);
		$this->nowPage = !empty($_GET[C('VAR_PAGE')]) ? intval($_GET[C('VAR_PAGE')]) : 1;
		if (!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
			$this->nowPage = $this->totalPages;
		}
		$this->firstRow = $this->listRows * ($this->nowPage - 1);
	}

	public function setConfig($name, $value) {
		if (isset($this->config[$name])) {
			$this->config[$name] = $value;
		}
	}

	/**
	  +----------------------------------------------------------
	 * 分页显示输出
	  +----------------------------------------------------------
	 * @access public
	  +----------------------------------------------------------
	 */
	public function show() {
		if ($this->totalPages <= 1)
			return '';
		$p = C('VAR_PAGE');
		$url = $_SERVER['REQUEST_URI'] . (strpos($_SERVER['REQUEST_URI'], '?') ? '' : "?") . $this->parameter;
		$parse = parse_url($url);
		if (isset($parse['query'])) {
			parse_str($parse['query'], $params);
			unset($params[$p]);
			$url = $parse['path'] . '?' . http_build_query($params) . "&";
		}
		//TODO 分页bug
		$currentPosition = ceil($this->rollPage / 2) - 1;
		$maxSpace = ceil($this->rollPage / 2) - 1;
		$preRow = $this->nowPage - $currentPosition;
		$nextRow = $this->nowPage + $currentPosition;
		if ($preRow < 1)
			$preRow = 1;
		if ($nextRow > $this->totalPages)
			$nextRow = $this->totalPages;

		//上下翻页字符串
		if ($this->nowPage == 1) {
			$prePage = "<a href='#;' class='p_disabled' disabled>" . $this->config['prev'] . "</a>";
		} else {
			$prePage = "<a href='" . $url . $p . "=" . ($this->nowPage - 1) . "'>" . $this->config['prev'] . "</a>";
		}
		if ($this->nowPage == $this->totalPages) {
			$nextPage = "<a href='#;' class='p_disabled' disabled>" . $this->config['next'] . "</a>";
		} else {
			$nextPage = "<a href='" . $url . $p . "=" . ($this->nowPage + 1) . "'>" . $this->config['next'] . "</a>";
		}

		//首页
		$theFirst = "";
		if ($preRow - 1 > $maxSpace) {
			$theFirst .= "<a href='" . $url . $p . "=1'>1</a> ";
			$theFirst .= '… ';
		} else {
			for ($i = 1; $i < $preRow; $i++) {
				$theFirst .= "<a href='" . $url . $p . "=" . $i . "'>" . $i . '</a> ';
			}
		}
		//1,2,3,4,5
		$linkPage = "";
		for ($i = $preRow; $i <= $nextRow; $i++) {
			if ($this->nowPage == $i)
				$linkPage .= '<strong class="p_curr">' . $i . '</strong> ';
			else
				$linkPage .="<a href='" . $url . $p . "=" . $i . "'>" . $i . '</a> ';
		}
		//尾页
		$theEnd = "";
		if ($this->totalPages - $nextRow > $maxSpace) {
			$theEnd .= '... ';
			$theEnd .= "<a href='" . $url . $p . "=" . $this->totalPages . "'>" . $this->totalPages . '</a> ';
		} else {
			for ($i = $nextRow + 1; $i <= $this->totalPages; $i++) {
				$theEnd .= "<a href='" . $url . $p . "=" . $i . "'>" . $i . '</a> ';
			}
		}
		$pageStr = str_replace(
			array('%header%', '%nowPage%', '%totalRow%', '%totalPage%', '%first%', '%prePage%', '%linkPage%', '%nextPage%', '%end%'), array($this->config['header'], $this->nowPage, $this->totalRows, $this->totalPages, $theFirst, $prePage, $linkPage, $nextPage, $theEnd), $this->config['theme']);
		return $pageStr;
	}

}

?>